package main

import (
	"context"
	"embed"
	go_runtime "runtime"

	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/menu"
	"github.com/wailsapp/wails/v2/pkg/menu/keys"
	"github.com/wailsapp/wails/v2/pkg/options"
	"github.com/wailsapp/wails/v2/pkg/options/assetserver"
	"github.com/wailsapp/wails/v2/pkg/options/mac"
	"github.com/wailsapp/wails/v2/pkg/options/windows"
	"github.com/wailsapp/wails/v2/pkg/runtime"
)

//go:embed all:frontend/dist
var assets embed.FS

func main() {
	err := InitConfigInfo()
	if err != nil {
		PanicErr(err)
	}

	err = InitDataSource()
	if err != nil {
		PanicErr(err)
	}

	// Create an instance of the app structure
	app := NewApp()
	runtimeExt := NewRuntimeExt()

	menus := menu.NewMenu()

	FileMenu := menus.AddSubmenu("File")
	FileMenu.AddText("Setting", keys.CmdOrCtrl(","), func(m *menu.CallbackData) {
		runtime.EventsEmit(app.ctx, "router", "/setting")
	})

	FileMenu.AddSeparator()
	FileMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) {
		runtime.Quit(app.ctx)
	})

	menus.AddSubmenu("123")

	if go_runtime.GOOS == "darwin" {
		menus.Append(menu.EditMenu()) // on macos platform, we should append EditMenu to enable Cmd+C,Cmd+V,Cmd+Z... shortcut
	}

	// Create application with options
	err = wails.Run(&options.App{
		Title:  "idea-note",
		Width:  1200,
		Height: 1080,
		AssetServer: &assetserver.Options{
			Assets:  assets,
			Handler: InitEchoFileLoader(),
		},
		Menu:             menus,
		BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
		OnStartup: func(ctx context.Context) {
			app.startup(ctx)
			runtimeExt.startup(ctx)
		},
		Bind: []interface{}{
			app,
			runtimeExt,
		},
		Mac: &mac.Options{
			Appearance: mac.NSAppearanceNameAqua, // 禁用暗黑模式
			TitleBar:   mac.TitleBarHiddenInset(),
		},
		Windows: &windows.Options{},
	})
	if err != nil {
		println("Error:", err.Error())
	}
}
